package com.xinzhou.demo.service;

import com.mike.module.datas.mapperImpl.CommonMapperImpl;
import com.xinzhou.demo.entity.EntityMike;
import com.xinzhou.demo.entity.EntityTest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @ClassName ServiceMike
 * @Description TODO
 * @Author Mike
 * @Date 2019/5/31 19:43
 * @Version 1.0
 */
@Service
public class ServiceMike {
    private final
    CommonMapperImpl commonMapperImpl;

    private static final String sql_select = "select * from test1 where id = {id}";
    private static final String sql_insert = "insert into test1 (name) values({name})";
    private static final String sql_update = "update test1 set name = {name} where id = {id}";
    private static final String sql_delete = "delete from test1 where name = {name}";

    @Autowired
    public ServiceMike(CommonMapperImpl commonMapperImpl) {
        this.commonMapperImpl = commonMapperImpl;
    }

    public Object test(EntityMike entityMike){

        String source = entityMike.getSource();
//        String type = entityMike.getType();
        String sql = entityMike.getSql();
        String type = sql.trim().split(" ")[0];




            switch(type){
                case "select":
                    return commonMapperImpl.selectListMap(source,sql);
                case "insert":
                    return commonMapperImpl.insertData(source,sql);
                case "update":
                    return commonMapperImpl.updateData(source,sql);
                case "delete":
                    return commonMapperImpl.deleteData(source,sql);
            }

        return null;
    }

    public Map<String,Object> entity(EntityMike entityMike){
        String sql = entityMike.getSql();
        String source = entityMike.getSource();
        return commonMapperImpl.selectObject(null,sql);
    }
}
